//
//  KVStatusesViewModel.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/23.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit
import SVProgressHUD

class KVStatusesViewModel: NSObject {
    ///单例微博管理者
    static let sharedStatuses: KVStatusesViewModel = KVStatusesViewModel()
    ///标记是否在下载
    var isLoading: Bool = false
    /*
    arameter since_id: 加载大于since_id大的微博, 默认为0
    parameter max_id:   加载小于或等于max_id的微博, 默认为0
    */
    func loadStatuses(since_id: Int64,max_id: Int64, completed: (statuses: [KVStatuses]?,error: NSError?) -> Void) {
    //https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00ttMxKGLaI1sC57ab3f866cZlLkRB
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let access_token: String = (KVUserAccountViewModel.sharedUserAccountViewModel.userAccount?.access_token)!
        //判断是否正在刷新
        if isLoading {
            print("正在加载请耐心等待")
            SVProgressHUD.showErrorWithStatus("加载错误")
            return
        }
        self.isLoading = true

        
        var parameters: [String: AnyObject] = [
            "access_token": access_token,
//            "count": 5
        ]
        
        /// 如果传入了since_id count
        if since_id > 0 {
            parameters["since_id"] = NSNumber(longLong: since_id)

        } else if max_id > 0 {
            parameters["max_id"] = NSNumber(longLong: max_id - 1)
        }
        
        KVNetworkTool.sharedNetworkTool.GET(urlString, parameters: parameters, progress: { (_) -> Void in
            }, success: { (_, result) -> Void in
                let result = result as! [String: AnyObject]
                let statuses = result["statuses"] as! [[String: AnyObject]]
                var desArr = [KVStatuses]()
            
                for status in statuses {
                    let statusModel = KVStatuses(dict: status)
                    desArr.append(statusModel)
                }
                completed(statuses: desArr, error: nil)
                self.isLoading = false
            }) { (_, error) -> Void in
                print(error)
                self.isLoading = false
                completed(statuses: nil, error: error)
        }
     }
    
    //发送微博 
    func sendStatuses(status: String,completedCallBack: (error: NSError?) -> Void) {
        //https://api.weibo.com/2/statuses/update.json
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let access_token = KVUserAccountViewModel.sharedUserAccountViewModel.userAccount?.access_token
        let parameters: [String: AnyObject] = ["access_token": access_token!, "status": status]
        
        KVNetworkTool.sharedNetworkTool.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            print("result:\(result)")
            completedCallBack(error: nil)
            }) { (_, error) -> Void in
                completedCallBack(error: error)
        }
    }
    
}
